package com.chhuang.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.chhuang.core.mapper.IBaseMapper;
import com.chhuang.core.model.Persistable;
import com.chhuang.core.service.IBaseService;

public abstract class BaseServiceImpl<TMapper extends IBaseMapper<TPo>, TPo extends Persistable> implements IBaseService <TMapper, TPo> {

	@Autowired
	private TMapper mapper;

	@Override
	public TPo findByPrimaryKey(String id) throws Exception {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(TPo entity) throws Exception {
		return mapper.insert(entity);
	}

	@Override
	public int saveSelective(TPo entity) throws Exception {
		return mapper.insertSelective(entity);
	}

	@Override
	public int saveOrUpdate(TPo entity) throws Exception {
		int total = 0;
		if(entity!=null && entity.getId()!=null && !"".equals(entity.getId())){
			total = mapper.updateByPrimaryKey(entity);
		}else{
			total = mapper.insert(entity);
		}
		return total;
	}

	@Override
	public int saveOrUpdateSelective(TPo entity) throws Exception {
		int total = 0;
		if(entity!=null && entity.getId()!=null && !"".equals(entity.getId())){
			total = mapper.updateByPrimaryKeySelective(entity);
		}else{
			total = mapper.insertSelective(entity);
		}
		return total;
	}

	@Override
	public int update(TPo entity) throws Exception {
		return mapper.updateByPrimaryKey(entity);
	}

	@Override
	public int updateSelective(TPo entity) throws Exception {
		return mapper.updateByPrimaryKeySelective(entity);
	}
	
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return mapper.deleteByPrimaryKey(id);
	}
}
